package com.yupi.springbootinit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.common.ToolUtils;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.mapper.DeptMapper;
import com.yupi.springbootinit.mapper.UserSignStateMapper;
import com.yupi.springbootinit.model.dto.UserSignRequest;
import com.yupi.springbootinit.model.entity.Dept;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.service.UserService;
import com.yupi.springbootinit.service.UserSignStateService;
import com.yupi.springbootinit.utils.DateUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (UserLoginState)表控制层
 *
 * @author makejava
 * @since 2024-03-07 14:11:04
 */
@RestController
@RequestMapping("userSignState")
public class UserSignStateController {
    /**
     * 服务对象
     */
    @Resource
    private UserSignStateService userSignStateService;

    @Resource
    private UserSignStateMapper userSignStateMapper;

    @Resource
    private UserService userService;

    @Resource
    private DeptMapper deptMapper;
    /**
     * 后端只需要传入的参数保存就可以了，这样后端逻辑简单点，前端的效果会更好
     */
    @PostMapping("/sign")
    public BaseResponse<String> sign(@RequestBody UserSignRequest userSignRequest, HttpServletRequest request) {
        if (userSignRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        userSignStateService.sign(loginUser,userSignRequest);
        return ResultUtils.success("ok");
    }

    /**
     * 返回需要签到的地理位置,gps定位和范围,以及签到的时间范围和现实时间
     */
    @GetMapping("/signLocation")
    public BaseResponse<Dept> signLocation( HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Integer deptId = loginUser.getDeptId();
        QueryWrapper<Dept> deptQueryWrapper = new QueryWrapper<>();
        deptQueryWrapper.eq("id",deptId);
        Dept dept = deptMapper.selectOne(deptQueryWrapper);
        if (dept != null){
            Date signInTime = dept.getSignInTime();
            Date signOutTime = dept.getSignOutTime();
            Date date = new Date();
            dept.setSignInTimeSFM(DateUtils.changeDateToSFM(signInTime));
            dept.setSignOutTimeSFM(DateUtils.changeDateToSFM(signOutTime));
            dept.setNowTimeSFM(DateUtils.changeDateToSFM(date));
        }
        return ResultUtils.success(dept);
    }

    /**
     * 获取用户当前的签到状态,签到/签退?
     * 判断是否是签到还是签退,是查询今天有没有签到记录
     */
    @GetMapping("/signStation")
    public BaseResponse<String> signStation( HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        Long id = loginUser.getId();
        Integer deptId = loginUser.getDeptId();
        List<Map<String,Object>> res = userSignStateMapper.signStation(id,deptId);
        if (res == null || res.size() == 0){
            return ResultUtils.success("signIn");
        }else if (res.get(0).get("signOutTime") == null){
            return ResultUtils.success("signOut");
        }else {
            return ResultUtils.success("alreadySign");
        }
    }
    /**
     * 根据用户的gps定位逆编码得到具体定位
     */
    @GetMapping("/getDetailLocation")
    public BaseResponse<String> getDetailLocation(String gpsLocation) throws IOException {
        String locationByGps = ToolUtils.getLocationByGps(gpsLocation);
        return ResultUtils.success(locationByGps);
    }

    /**
     * 查询签到的接口 查询页
     * @param queryDate
     * @param request
     * @return
     * @throws IOException
     */
    @GetMapping("getSignStateByDate")
    public BaseResponse<List<Map<String,Object>>> getSignStateByDate(String queryDate,HttpServletRequest request) throws IOException {
        User loginUser = userService.getLoginUser(request);
        Long id = loginUser.getId();
        Integer deptId = loginUser.getDeptId();
        Map<String,Object> userSignState = userSignStateMapper.getSignStateByDate(queryDate,id,deptId);
        QueryWrapper<Dept> deptQueryWrapper = new QueryWrapper<>();
        deptQueryWrapper.eq("id",deptId);
        Dept dept = deptMapper.selectOne(deptQueryWrapper);
        List<Map<String,Object>> resMap = userService.getMapUserState(userSignState,dept);
        //公司本身的签到签退
        return ResultUtils.success(resMap);
    }
    /**
     * 查询签到的接口 查询页(管理页)
     * @param queryDate
     * @return
     * @throws IOException
     */
    @GetMapping("getSignStateByDateAdmin")
    public BaseResponse<Map<String,Object>> getSignStateByDateAdmin(String queryDate,Integer userId) throws IOException {
        User user = userService.getById(userId);
        Long id = user.getId();
        Integer deptId = user.getDeptId();
        Map<String,Object> userSignState = null;
        if (userId != null && !userId.equals("")){
            User byId = userService.getById(userId);
            Integer deptId1 = byId.getDeptId();
            userSignState = userSignStateMapper.getSignStateByDate(queryDate,userId.longValue(),deptId1);
        }else{
             userSignState = userSignStateMapper.getSignStateByDate(queryDate,id,deptId);
        }
        return ResultUtils.success(userSignState);
    }

    @GetMapping("byTimePeriod")
    public BaseResponse<Map<String,Object>> searchSignStateByTimePeriod(String startTime,String endTime,HttpServletRequest request) throws IOException {
        Map<String,Object> map = userSignStateService.searchSignStateByTimePeriod(startTime,endTime,request);
        return ResultUtils.success(map);
    }
}

